package cn.nb12.test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.nb12.xbox.utils.ExchangeRateUtil;
import cn.nb12.xbox.utils.RequestUtil;

public class GetInfo {
	public static void main(String[] args) {
		String url="https://www.microsoft.com/zh-tw/store/p/playerunknowns-battlegrounds/c0mn5dn8kr3f";
		String response;
		try {
			Map<String,String> headers=new HashMap<>();
			headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0");
			headers.put("Content-Type", "application/json; charset=utf-8");
			response = RequestUtil.sendPost(url, "",headers,"UTF-8");
			Document doc = Jsoup.parse(response);
			Elements select2 = doc.select("meta[name='keywords']");
			System.out.println("游戏名："+select2.attr("content"));
			Elements links = doc.select("div.srv_microdata");
			if(links!=null && links.size()>0) {
				Element element = links.get(0);
				Elements select = element.select("meta");
				if(select!=null && select.size()==2) {
					Element price = select.get(0);
					Element currency = select.get(1);
					System.out.println(String.format("价格：%s:%s，折合人民币%s", currency.attr("content"),price.attr("content"),Double.parseDouble(price.attr("content"))*Double.parseDouble(ExchangeRateUtil.getExchangeRate(currency.attr("content"), "CNY"))));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void getInfo(String url) {
		String response;
		try {
			Map<String,String> headers=new HashMap<>();
			headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0");
			headers.put("Content-Type", "application/json; charset=utf-8");
			response = RequestUtil.sendPost(url, "",headers,"UTF-8");
			Document doc = Jsoup.parse(response);
			Elements select2 = doc.select("meta[name='keywords']");
			System.out.println("游戏名："+select2.attr("content"));
			Elements links = doc.select("div.srv_microdata");
			if(links!=null && links.size()>0) {
				Element element = links.get(0);
				Elements select = element.select("meta");
				if(select!=null && select.size()==2) {
					Element price = select.get(0);
					Element currency = select.get(1);
					System.out.println(String.format("价格：%s:%s，折合人民币%s", currency.attr("content"),price.attr("content"),Double.parseDouble(price.attr("content"))*Double.parseDouble(ExchangeRateUtil.getExchangeRate(currency.attr("content"), "CNY"))));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
